(function ($app) {
    $app.module('gtPartyApp').controller('zlgdCtrl', function ($scope, $http, $modal, $ocLazyLoad, $alert, $loading, $state, $partySelect, $stateParams, AuthUser,$bsRouterState) {
        $stateParams = $bsRouterState.$params($scope);

        //定义数据集合
        $scope.flowModel = {
            flowid: $stateParams.flowId == undefined ? "" : $stateParams.flowId,
            joinuserid: $stateParams.joinUserId == undefined ? "" : $stateParams.joinUserId
        };
        $scope.userModel = {};
        $scope.dataModel = {putrecordid: '', hasprecedure: null};
        $scope.boolList = [{dickey: 1, dicvalue: '是'}, {dickey: 0, dicvalue: '否'}];

        $scope.reqFileModel = {
            selectdata: {FileRefID: '', pageindex: 1, pagesize: 10, ptotal: 0},//查询参数：FileRefID为文件关联ID
            filetype: 3,//文件类型
            items: [],//列表数据地址
            readonly: false,//是否只能查看
            title: '附件（上传归档资料描件）',
            addFileButton: '上传'
        };

        $scope.loadData = function () {
            $http.get("../../api/partyExpand/getzlgd", {
                params: {
                    'flowId': $scope.flowModel.flowid,
                    'joinUserId': $scope.flowModel.joinuserid,
                    'userName':AuthUser.getUser().Name
                }
            }).then(function (result) {
                $scope.dataModel = result.data.item;
                $scope.reqFileModel.selectdata.FileRefID = $scope.dataModel.id;
            }, function () {
                $scope.showMsg('错误', '服务器错误');
            });
        };

        //获取数据
        $scope.loadUser = function () {
            $http.get("../../api/partyExpand/getJoinuserInfoById", {
                params: {
                    'id': $scope.flowModel.joinuserid
                }
            }).then(function (result) {
                $scope.userModel = result.data.item;
                $scope.loadData();
            }, function () {
                $scope.showMsg('错误', '服务器错误');
            });
        };

        //保存
        $scope.submitData = function (isflag) {
            if (!isflag) {
                $scope.showMsg('提示', '请填写相关信息');
                return false;
            }

            if (isflag) {

                if (confirm("确认提交？")) {
                    $loading.show();
                    $http({
                        method: "post",
                        url: "../../api/partyExpand/savezlgd",
                        data: {
                            dataModel: $scope.dataModel,
                            flowId: $scope.flowModel.flowid
                        }
                    }).then(function (result) {
                        $loading.hide();
                        if (result.data.success) {
                            $scope.showMsg('成功', result.data.msg);
                            //重新刷新列表
                            $scope.pagechange();

                            $scope.$emit("tabReloadData",{name:'peJoinUserProcessCtrl',data:1});
                        } else {
                            $scope.showMsg('失败', result.data.msg);
                        }

                    }, function (resp) {
                        $scope.showMsg('错误', '服务器错误');
                    });
                }
            }
        };
        $scope.loadUser();


        $scope.pagechange = function () {
            var inTab = $bsRouterState.$getRouteType() == 'tab';
            if (inTab) {
                $bsRouterState.$closeTab($scope);
            } else {
                window.history.back();
            }
        };

        $scope.showMsg = function (title, content) {
            $alert({
                title: title + '：',
                content: content,
                placement: 'top',
                type: 'info',
                show: true,
                duration: 3
            });

        };

    });
})(angular);